package com.yunhe.sms.dao.activity.impl;

import com.yunhe.commons.dao.impl.BaseDaoImpl;
import com.yunhe.commons.dto.Pageable;
import com.yunhe.sms.dao.activity.VoteItemDao;
import com.yunhe.sms.entity.activity.VoteItem;

import java.util.List;

/**
 * Created by Administrator on 2017/10/30 0030.
 */
public class VoteItemDaoImpl extends BaseDaoImpl<VoteItem> implements VoteItemDao {
    @Override
    public void save(VoteItem voteItem) {
        String sql = " insert into activity_vote_item(vote_id,name,img,detail,sort_no,votes,created_time,deleted) values(?,?,?,?,?,0,now(),0)";
        super.cud(sql,voteItem.getVoteId(),voteItem.getName(),voteItem.getImg(),voteItem.getDetail(),voteItem.getSortNo());
    }

    @Override
    public void delete(Long id) {
        String sql = " update activity_vote_item set deleted = 1 where id = ? ";
        super.cud(sql,id);
    }

    @Override
    public void update(VoteItem voteItem) {
        String sql = " update activity_vote_item set name=?,img=?,sort_no=? where id =? ";
        super.cud(sql,voteItem.getName(),voteItem.getImg(),voteItem.getSortNo(),voteItem.getId());
    }

    @Override
    public void updateVotes(Long id) {
        String sql = " update activity_vote_item set votes = votes+1 where id = ? ";
        super.cud(sql,id);
    }

    @Override
    public VoteItem find(Long id) {
        String sql = " select id,vote_id voteId,name,img,detail,votes,sort_no sortNo from activity_vote_item where deleted = 0 and id = ? ";
        return super.queryOne(sql,id);
    }

    @Override
    public List<VoteItem> findList(Long voteId) {
        String sql = " select id,vote_id voteId,name,img,detail,votes,sort_no sortNo from activity_vote_item where deleted = 0 and vote_id = ? ";

        return super.queryList(sql,voteId);
    }

    @Override
    public Pageable<VoteItem> findPage(Integer offset, Integer limit, Long voteId) {
        String listSql = " select id,vote_id voteId,name,img,detail,votes,sort_no sortNo from activity_vote_item where deleted = 0 and vote_id = ? ";
        String countSql = " select count(*) from activity_vote_item where deleted = 0 and vote_id = ?";

        return super.queryPage(listSql,countSql,offset,limit,voteId);
    }
}
